package tnakgame01;

/**
 * 子弹的射击
 */
public class Shot implements Runnable {
    int x;
    int y;
    int direct = 0;//子弹的初始方向
    int speed = 4;//子弹的速度,还可在休眠处控制速度
    boolean isLive = true;//子弹是否存活

    public Shot(int x, int y, int direct) {
        this.x = x;
        this.y = y;
        this.direct = direct;
    }

    @Override
    public void run() {//射击行为
        while (true) {
            //子弹休眠 20 毫秒
            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            //根据方向来改变x,y坐标
            switch (direct) {
                case 0://向上发射
                    y -= speed;
                    break;
                case 1://向下发射
                    y += speed;
                    break;
                case 2://向左发射
                    x -= speed;
                    break;
                case 3://向右发射
                    x += speed;
                    break;
            }
            //进行测试，输出子弹的x和y坐标
            System.out.println("x= " + x + ",y= " + y);
            //当子弹移动到面板的边界时，或击中敌人的坦克时，就销毁该子弹的线程
            if (!(x >= 0 && x <= 1200 && y >= 0 && y <= 900 && isLive)) {
                System.out.println("子弹线程退出");
                isLive = false;
                break;
            }
        }
    }
}
